﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class RuKuTongZhi : System.Web.UI.Page
    {
        protected string Notice = "";
        private string sql =
@"select 
rk.[ID]
,产品类别ID
,cp.[品名]+' '+cp.[钢级]+' '+cp.[规格] as 产品类别
,case
when 生产线 between 100 and  199 then '一分厂'
when 生产线=201 then '二分厂B1'
when 生产线=202 then '二分厂B2'
when 生产线=501 then '五分厂C1'
when 生产线=502 then '五分厂C2'
else '未知分厂'
end
as 生产单位

,yl.[名称] as [原料供货单位]
,rk.[序号] as [入库序号]
,rk.[总重量]
,rk.[总长度]
,rk.[总件数]
,rk.[是否合格]
,rk.[日期]
 from [产品入库单] as rk INNER JOIN
      [产品] as cp ON  rk.产品类别ID = cp.ID INNER JOIN 
      [原料供货单位] as yl ON rk.供货单位ID=yl.ID
";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                if ((object)Session["OperatorDID"] != null)
                {
                    int did = (int)Session["OperatorDID"];
                    if (did != 0)
                    {
                        //this.FenChang.Text = did.ToString();
                        //this.FenChang.Enabled = false;
                        if (did == 1)
                        {
                            this.FenChang.Items.Clear();
                            ListItem li1 = new ListItem();
                            li1.Value = "1";
                            li1.Text = "一分厂";
                            this.FenChang.Items.Add(li1);
   
                        }
                        else if (did == 2)
                        {
                            this.FenChang.Items.Clear();
                            ListItem li1 = new ListItem();
                            li1.Value = "2";
                            li1.Text = "二分厂";
                            this.FenChang.Items.Add(li1);

                            ListItem li2 = new ListItem();
                            li2.Value = "201";
                            li2.Text = "　二分厂B1";
                            this.FenChang.Items.Add(li2);

                            ListItem li3 = new ListItem();
                            li3.Value = "202";
                            li3.Text = "　二分厂B2";
                            this.FenChang.Items.Add(li3);

                        }
                        else if (did == 5)
                        {
                            this.FenChang.Items.Clear();
                            ListItem li1 = new ListItem();
                            li1.Value = "5";
                            li1.Text = "五分厂";
                            this.FenChang.Items.Add(li1);

                            ListItem li2 = new ListItem();
                            li2.Value = "501";
                            li2.Text = "　五分厂C1";
                            this.FenChang.Items.Add(li2);

                            ListItem li3 = new ListItem();
                            li3.Value = "502";
                            li3.Text = "　五分厂C2";
                            this.FenChang.Items.Add(li3);

                            this.GridView1.Columns[4].Visible = true;
                        }
                    }
                }
                this.GongHuo.DataBind();
                Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit2.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            int xuhao=0;
            int.TryParse(this.XuHao.Text, out xuhao);

            if (this.ChanPin.Text != "")
            {
                sql += " and rk.[产品类别ID]=" + this.ChanPin.Text;
            }
            else {
                //如果什么都没有选择，则默认为1
                sql += " and rk.[产品类别ID]=1 ";
            }
            if (this.FenChang.Text =="1")
            {
                sql += " and rk.[生产线] between 100 and 199";
            }
            else if (this.FenChang.Text == "2")
            {
                sql += " and rk.[生产线] between 200 and 299";
            }
            else if (this.FenChang.Text == "5")
            {
                sql += " and rk.[生产线] between 500 and 599";
            }
            else
            {
                sql += " and rk.[生产线]="+this.FenChang.Text;
            }

            if (this.GongHuo.Text != "0")
            {
                sql += " and rk.[供货单位ID]=" + this.GongHuo.Text.Replace("'", "");
            }
            if (xuhao>0)
            {
                sql += " and rk.[序号]="+xuhao;
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and rk.[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and rk.[日期]<='" + Dt.ToShortDateString() + "'";
            }
            sql += " order by rk.[ID] desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.GridView1.SelectedIndex = -1;
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "Exec ChanPin_In_Bill_Delete " + this.Button_Delete.CommandArgument;
            this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_Delete.CommandArgument = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Edit2.Enabled = true;
            this.Button_Edit2.OnClientClick = "parent.swin2_load('/Fc/产品判定入库.aspx?id="
                + this.Button_Delete.CommandArgument + "',840,700,'yes');";

            if (!(bool)this.GridView1.SelectedDataKey[1])
            {
                this.Button_Delete.Enabled = true;
            }
            else
            {
                this.Button_Delete.Enabled = false;
            }
        }
    }
}
